/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Configuration;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using ELE.EnterpriseLibrary.Data.Configuration.Design.Properties;
using System.ComponentModel.Design;

namespace ELE.EnterpriseLibrary.Data.Configuration.Design
{
  /// <summary>
  /// Represents the design manager that will register the design time information for the O/R Mapping Application Block.
  /// </summary>
  public sealed class ObjectMappingConfigurationDesignManager : ConfigurationDesignManager
  {
    /// <summary>
    /// Initialize a new instance of the <see cref="ObjectMappingConfigurationDesignManager"/> class.
    /// </summary>
    public ObjectMappingConfigurationDesignManager()
    {
    }

    /// <summary>
    /// Registers the caching design manager into the environment.
    /// </summary>
		/// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
    public override void Register(IServiceProvider serviceProvider)
    {
			ObjectMappingNodeMapRegistrar nodeRegistrar = new ObjectMappingNodeMapRegistrar(serviceProvider);
			nodeRegistrar.Register();
			ObjectMappingCommandRegistrar cmdRegistrar = new ObjectMappingCommandRegistrar(serviceProvider);
			cmdRegistrar.Register();
    }

		/// <summary>
		/// Opens the caching configuration from an application configuration file.
		/// </summary>
		/// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		/// <param name="rootNode">The <see cref="ConfigurationApplicationNode"/> of the hierarchy.</param>
		/// <param name="section">The caching configuration section or null if no section was found.</param>
		protected override void OpenCore(IServiceProvider serviceProvider, ConfigurationApplicationNode rootNode, ConfigurationSection section)
		{
			if (null != section)
			{
				ObjectMappingSettingsNodeBuilder builder = new ObjectMappingSettingsNodeBuilder(serviceProvider, (ObjectMappingSettings)section);
				rootNode.AddNode(builder.Build());
			}
		}

		/// <summary>
		/// Gets a <see cref="ConfigurationSectionInfo"/> object containing the ObjectMapping Block's configuration information.
		/// </summary>
		/// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		/// <returns>A <see cref="ConfigurationSectionInfo"/> object containing the ObjectMapping Block's configuration information.</returns>
		protected override ConfigurationSectionInfo GetConfigurationSectionInfo(IServiceProvider serviceProvider)
		{
			ConfigurationNode rootNode = ServiceHelper.GetCurrentRootNode(serviceProvider);
			ObjectMappingSettingsNode node = null;
			if (rootNode != null) node = (ObjectMappingSettingsNode)rootNode.Hierarchy.FindNodeByType(rootNode, typeof(ObjectMappingSettingsNode));
			ObjectMappingSettings cacheSection = null;
			if (node == null)
			{
        cacheSection = null;
			}
			else
			{
				ObjectMappingSettingsBuilder builder = new ObjectMappingSettingsBuilder(serviceProvider, node);
				cacheSection = builder.Build();
			}
			return new ConfigurationSectionInfo(node, cacheSection, ObjectMappingSettings.SectionName);
		}
  }
}